<?php
class AdAction extends AdminAction {
    //广告管理
	public function index(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$ad=M('Ad');
		$list = $ad->select();
		$this->assign("list",$list);
		$this->display();
	}
	
	//添加广告
	public function addad(){
	   $isLogin = session(C('ADMIN_AUTH_KEY'));
	   if(!$isLogin) $this->redirect('User/login');
	   if($_POST){
	   	 import("ORG.Util.UploadFile");
	        //导入上传类
	        $upload = new UploadFile();
	        //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //设置上传文件类型
	        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
	        //设置附件上传目录
	        $upload->thumb = true;
		    $upload->imageClassPath = 'ORG.Util.Image';
	        $upload->thumbPrefix = 'm_';
	        $upload->thumbMaxWidth = 203;
		    $upload->thumbMaxHeight = 69;
	        $upload->savePath = './Public/Admin/Ad/';
	        if (!$upload->upload($_FILES)) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	        } else {
	            //取得成功上传的文件信息
	            $uploadList = $upload->getUploadFileInfo();
	        }
	   	     $_POST['adimg']=$uploadList[0]['savename'];
	       // print_r($uploadList);exit();
	        $cond=$_POST;
		   	$cond['_column'] = array('adname','adimg','orders','adurl');
			foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
			$topic=D('Ad');
			if($topic->create($data)){
				if($topic->add($data)!=false){
				  $this->redirect('Ad/index');
				}
			}else{
				  $this->assign('erorsta','1');
				  $this->assign('error',$topic->getError());
			}
	    }
	   $this->display();
	}
    
	//编辑广告
 	public function edit(){
	   $isLogin = session(C('ADMIN_AUTH_KEY'));
	   if(!$isLogin) $this->redirect('User/login');
	    $fid['adid']=$_GET['adid'];
	    $model=D('Ad');
	    $data=$model->where($fid)->find();
	    $this->assign('data',$data);
	    if($_POST){
	    	if(!empty($_FILES['adimg']['name'])){
	    	import("ORG.Util.UploadFile");
	        //导入上传类
	        $upload = new UploadFile();
	        //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //设置上传文件类型
	        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
	        $upload->thumb = true;
		    $upload->imageClassPath = 'ORG.Util.Image';
	        $upload->thumbPrefix = 'm_';
	        $upload->thumbMaxWidth = 203;
		    $upload->thumbMaxHeight = 69;
	        //设置附件上传目录
	        $upload->savePath = './Public/Admin/Ad/';
	        if (!$upload->upload($_FILES)) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	        } else {
	        	unlink('./Public/Admin/Ad/'.$data['adimg']);
	        	unlink('./Public/Admin/Ad/m_'.$data['adimg']);
	            //取得成功上传的文件信息
	            $uploadList = $upload->getUploadFileInfo();
	        }
	   	     $_POST['adimg']=$uploadList[0]['savename'];
	    	}
	    	$info=$_POST;
		    $info['adid']=$_GET['adid'];
		    if($model->create($info)){
		   	 $model->save($info);
		   	 $this->redirect('Ad/index');
		   	}else{
					  $this->assign('erorsta','1');
					  $this->assign('error',$model->getError());
				}
	    }

    $this->display();
   }
   
	  public function move(){
	  $isLogin = session(C('ADMIN_AUTH_KEY'));
	  if(!$isLogin) $this->redirect('User/login');
	    if($_GET['adid']){
		    $fid['adid']=$_GET['adid'];
		    $model=D('Ad');
		    $name=$model->field('adimg')->where($fid)->find();
		    if($name){
			    $model->where($fid)->delete();
			    unlink('./Public/Admin/Ad/'.$name['adimg']);
			    unlink('./Public/Admin/Ad/m_'.$name['adimg']);
			    $this->redirect('Ad/index');
		    }else{
				  $this->redirect('Ad/index');
			}
	    }else{
			$this->redirect('Ad/index');
	    }
	    
	  }
   
	//禁用
	public function forbidden()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$adid = $_POST['adid'];
		
		if($adid)
		{
			$map['adid'] = $adid;
			//$map["status"] = 1;
			$data["status"] = 0;
			$res = M('Ad')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	//启用
	public function enable()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$adid = $_POST['adid'];
		
		if($adid)
		{
			$map['adid'] = $adid;
			//$map["status"] = 0;
			$data["status"] = 1;
			$res = M('Ad')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
}